package com.fieldchain.ex.fa100.ui.personal.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.mvp.presenter.LoginPresenter;
import com.fieldchain.ex.fa100.mvp.view.LoginView;

import com.fieldchain.ex.fa100.utils.common.InputAnimation;
import com.fieldchain.ex.fa100.utils.screen.ScreenUtils;
import com.fieldchain.ex.fa100.utils.share.ShareManager;
import com.fieldchain.ex.fa100.utils.share.ShareUtils;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/3
 */
public class LoginActivity extends MyBaseActivity<LoginPresenter, LoginView> implements LoginView, View.OnClickListener {
    private RelativeLayout reaBack;
    private ImageView ivBac;
    private EditText etPhone;
    private EditText etPwd;
    private TextView tvForget;
    private RelativeLayout reaImgcode;
    private ImageView ivImgcode;
    private EditText etImagecode;
    private TextView tvSure;
    private LinearLayout linBottom;
    private TextView tvSms;
    private TextView tvRegist;


    private InputAnimation animation;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        ivBac = findViewById(R.id.iv_bac);
        etPhone = findViewById(R.id.et_phone);
        etPwd = findViewById(R.id.et_pwd);
        tvForget = findViewById(R.id.tv_forget);
        reaImgcode = findViewById(R.id.rea_imgcode);
        ivImgcode = findViewById(R.id.iv_imgcode);
        etImagecode = findViewById(R.id.et_imagecode);
        tvSure = findViewById(R.id.tv_sure);
        linBottom = findViewById(R.id.lin_bottom);
        tvSms = findViewById(R.id.tv_sms);
        tvRegist = findViewById(R.id.tv_regist);
    }

    @Override
    public void setValue() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        ShareUtils.getInstance().clearAll();
        String androidID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        String id = androidID + Build.SERIAL;
        ShareManager.getInstance().saveDeviceId(id);
        ShareManager.getInstance().saveWidth(ScreenUtils.getScreenWidth(mContext));
        int statusBarHeight = ScreenUtils.getStatusBarHeight(mContext);
        ShareManager.getInstance().saveHeight(ScreenUtils.getScreenHeight(mContext) - statusBarHeight);
        ivImgcode.setOnClickListener(this);
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        tvForget.setOnClickListener(this);
        tvRegist.setOnClickListener(this);
        tvSms.setOnClickListener(this);
        setInput();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void onPause() {
        super.onPause();
        etImagecode.setText("");
        etPhone.setText("");
        etPwd.setText("");
    }

    @Override
    public void loginResult(int type) {
        if (type == 200) {
          finish();
        } else if (type == 511) {
            presenter.getVer();
            reaImgcode.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.iv_imgcode:
                presenter.getVer();
                break;
            case R.id.tv_forget:
                startActivity(new Intent(mContext, ForgetActivity.class));
                break;
            case R.id.tv_regist:
                startActivity(new Intent(mContext, RegistPhoneActivity.class));
                break;
            case R.id.tv_sure:
                presenter.login(etPhone.getText().toString(), etPwd.getText().toString(), etImagecode.getText().toString());
                break;

            case R.id.tv_sms:
                startActivity(new Intent(mContext, LoginSMSActivity.class));
                finish();
                break;
        }
    }

    public void setInput() {
        animation = new InputAnimation();
        animation.attachView((Activity) mContext, 70, etPwd, linBottom, ivBac);
    }

    @Override
    public void verImage(Bitmap bit) {
        ivImgcode.setImageBitmap(bit);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        animation.detachView();
        animation = null;
    }
}
